<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Loanapply extends Model
{
    protected $table = 'loanapply';
    public $timestamps = false;
    protected $guarded = [
        'name', 'money', 'weixin', 'ishavehouse', 'apply_time', 'updated_at', 'deleted_at'
    ];

    public function getData($search = [], $page = 1, $length = 20){
        $loanapplyList = array();
        $loanapplyList['pageData'] = array();
        $date = date('Y-m-d H:i:s', time());

        $sql = \DB::table($this->table)
            ->where('check_status',0)
            ->whereNull('deleted_at');

        if(isset($search['name'])){
            $sql->where('name', 'like', '%'.$search['name'].'%');
        }
        if(isset($search['weixin'])){
            $sql->where('weixin', 'like', '%'.$search['weixin'].'%');
        }

        $loanapplyList['pageData']['count']     = $sql->count();
        $loanapplyList['pageData']['lastPage']  = ceil($loanapplyList['pageData']['count'] / $length);

        $loanapplyList['list'] = $sql->orderBy('id', 'desc')
            ->skip(($page-1) * $length)->take($length)
            ->get();

        return $loanapplyList;
    }

    public function saveData($data){
        return \DB::table($this->table)->insert($data);
    }

    public function setData($id,$data){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }

    public function delData($id,$data){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }

}